package com.example.jiaju.service.impl;

import com.example.jiaju.dataobject.Host;
import com.example.jiaju.dataobject.UserHost;
import com.example.jiaju.enums.ResultEnum;
import com.example.jiaju.exception.SMException;
import com.example.jiaju.repository.HostRepository;
import com.example.jiaju.repository.UserHostRepository;
import com.example.jiaju.service.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HostServiceImpl implements HostService{
    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private UserHostRepository userHostRepository;

    @Override
    public Host save(Host host) {
        return hostRepository.save(host);
    }

    @Override
    public Host findByIEEE(String IEEE) {
        return hostRepository.findByIEEE(IEEE);
    }

    @Override
    public Host updateName(String phone, String IEEE, String newName) {
        UserHost userHost=userHostRepository.findByUserIdAndHostId(phone,IEEE);
        if (userHost==null){
            throw new SMException(ResultEnum.USER_HOST_NOT_FOUND);
        }

        Host host=hostRepository.findByIEEE(IEEE);
        host.setName(newName);
        hostRepository.save(host);
        return host;
    }
}
